package com.qcby.types.pojo;

import cn.hutool.json.JSONUtil;
import com.qcby.types.exception.IResponse;


import java.io.Serializable;

public class Result<T> implements Serializable {

    private final String code;
    private final String subCode;
    private final String message;
    private final Boolean success;
    private final T data;

    public Result() {
        this(null, null, null);
    }

    private Result(String code, String message, Boolean success) {
        this(code, message, success, null);
    }

    private Result(String code, String message, Boolean success, T data) {
        this(code, message, success, data, code);
    }

    private Result(String code, String message, Boolean success, T data, String subCode) {
        this.code = code;
        this.message = message;
        this.success = success;
        this.data = data;
        this.subCode = subCode;
    }

    public static Result createError() {
        return createError(BaseResponse.INTERNAL_ERROR.getCode(),
                BaseResponse.INTERNAL_ERROR.getMessage());
    }

    public static Result createError(String message) {
        return createError(BaseResponse.INTERNAL_ERROR.getCode(), message);
    }

    public static Result createError(IResponse IResponseCode) {
        return createError(IResponseCode.getCode(), IResponseCode.getMessage());
    }

    public static Result createError(String code, String message) {
        return createError(code, message, null);
    }

    public static <T> Result createError(String code, String message, T data) {
        return new Result(code, message, false, data);
    }

    public static <T> Result createError(String code, String message, String subCode, T data) {
        return new Result(code, message, false, data, subCode);
    }

    public static Result createSuccess() {
        return createSuccess(null);
    }

    public static <T> Result createSuccess(T data) {
        return createSuccess(BaseResponse.SUCCESS.getCode(),
                BaseResponse.SUCCESS.getMessage(),
                data);
    }

    public static <T> Result createSuccess(String code, String message, T data) {
        return new Result(code, message, true, data);
    }

    public String getCode() {
        return code;
    }

    public String getSubCode() {
        return subCode;
    }

    public String getMessage() {
        return message;
    }

    public Boolean isSuccess() {
        return success;
    }

    public T getData() {
        return data;
    }

    @Override
    public String toString() {
        return JSONUtil.toJsonStr(this);
    }
}
